package com.jinyijia.admin.interfaces;

import com.alibaba.fastjson.JSONObject;
import com.jinyijia.admin.config.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestOperations;

import java.util.Objects;

/**
 * Created by yangrd on 2018/3/11
 * 类目接口
 **/
@RestController
@RequestMapping(Constant.Path.SHOPS)
public class ShopController {

    @Autowired
    private RestOperations restOperations;

    @Value("${jinYiJia.api.shops}")
    private String apiPath;

    @PostMapping
    public ResponseEntity save(@RequestBody JSONObject jsonObject) {
        // TODO 更改用户角色
      /*  jsonObject.getJSONObject("sellerInfo").getString("id");
        restOperations.put(accountApiPath,jsonObject);*/

        return restOperations.postForEntity(apiPath, jsonObject, JSONObject.class);
    }

    @GetMapping
    public ResponseEntity findAll(@RequestParam(required = false) String name,
                                  @RequestParam(defaultValue = "0") Long page,
                                  @RequestParam(defaultValue = "10") Long size) {
        String url = Objects.nonNull(name) ? apiPath + "?name=" + name + "&" : apiPath + "?";
        return restOperations.getForEntity(url + "page={1}&size={2}", JSONObject.class, page, size);
    }

}
